<?php
class Quanlybaiviet extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('checkuser');
            $this->checkuser->CheckUserLogin();  
    }
    
    public function index()
    {
        $this->loadMainView();
    }
    
    private function loadMainView()
    {
        $this->load->model('admin/managebaiviet','baiviet');
        $temp['title'] = 'Quản lý bài viết';
        $temp['template'] = 'admin/quanlybaiviet';
        $temp['data']['info'] = 'Quản lý bài viết';
        $temp['data']['baiviet'] = $this->baiviet->LoadTatCaBaiViet();
        $this->load->view('admin/template/layout', $temp);
    }
    
    public function vietBaiMoi()
    {
        $this->load->model('nkdefault', 'default');
        $temp['title'] = 'Thêm bài viết mới';
        $temp['template'] = 'admin/vietbaimoi';
        $temp['data']['info'] = 'Thêm bài viết mới';
        $temp['data']['danhmuc'] = $this->default->loadDanhMuc(true);
        $this->load->view('admin/template/layout', $temp);
    }
    
    public function xoaBaiViet()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : "";
        $madanhmuc = isset($_GET['madanhmuc']) ? $_GET['madanhmuc'] : "";
        if($id != "" && $madanhmuc != "")
        {   
            $this->load->model('admin/managebaiviet','baiviet');
            $this->baiviet->ID = $id;
            $this->baiviet->MaDanhMuc = $madanhmuc;
            $result = $this->baiviet->XoaBaiViet();
            if($result) $this->loadMainView();
            
        }
    }
    
    public function themBaiVietMoi()
    {       
        $tieude = $this->input->post('tieude');
        if($tieude != "")
        {
            $this->load->model('admin/managebaiviet','baiviet');
            $this->load->model('nkdefault', 'default');
            $randomName = $this->default->createRandomStr();
            $danhmuc = $this->input->post('dropDownDanhMuc');
            $isInputLink = $this->input->post('chkInputLink');
            $linkImage = "";
            $dir_upload = 'assets/imgs/';
            if($isInputLink == 'yes')
            {
                $linkImage = $this->input->post('linkimage');
            }
            else
            {
                //get file info
                $type = pathinfo($_FILES["uploadImage"]["name"], PATHINFO_EXTENSION);
                $orginalName = basename($_FILES["uploadImage"]["name"], "." . $type);
                $tenFile = $orginalName . "_" .$randomName. "." . $type;
                $linkImage = base_url('assets/imgs/'  . $tenFile);
                //$regexImage = "/\.(gif|jpg|jpeg|tiff|png)$/";
            }
            $arrDanhMuc = explode('-', $danhmuc);            
            $madanhmuc_cha = $arrDanhMuc[0];            
            $madanhmuc = $arrDanhMuc[1];            
            $noidungbaiviet = $_POST['editor'];
            $this->baiviet->MaDanhMuc = $madanhmuc;
            $this->baiviet->MaDanhMuc_Cha = $madanhmuc_cha;            
            $this->baiviet->TieuDe = $tieude;
            $this->baiviet->NoiDungBaiViet_Viet = $noidungbaiviet;
            $this->baiviet->NgayVietBai = date('Y-m-d H:i:s');
            $this->baiviet->HienThi = '1';
            $this->baiviet->ImageLink = $linkImage;
            
            $result = $this->baiviet->ThemBaiVietMoi();
            if($result)
            {
                //if user used upload file, the function will move a file to the folder assets/imgs
                if($isInputLink == 'no') move_uploaded_file($_FILES["uploadImage"]["tmp_name"], $dir_upload  . $tenFile);  
                $this->loadMainView();
            }
        }
    }
    
    public function LayBaiViet()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : "";
        if($id != "")
        {
            $this->load->model('admin/managebaiviet','baiviet');
            $this->load->model('nkdefault', 'default');
            $temp['title'] = 'Xem bài viết';
            $temp['template'] = 'admin/xembaiviet';
            $temp['data']['info'] = 'Quản lý bài viết';
            $where = array('id' => $id, 'hienthi' => '1');
            $objBaiViet = $this->baiviet->LoadBaiViet($where);
            $temp['data']['baiviet'] = $objBaiViet->result_array();
            $temp['data']['danhmuc'] = $this->default->loadDanhMuc(true);
            $this->load->view('admin/template/layout', $temp);
        }        
    }
    
    public function CapNhatBaiViet()
    {
        $id = $this->input->post('id');
        $tieude = $this->input->post('tieude');
        if($id != "" && $tieude != "")
        {
            $this->load->model('admin/managebaiviet','baiviet');
            $madanhmuc = $this->input->post('madanhmuc');
            $noidungbaiviet = $this->input->post('noidungbaiviet');
            $madanhmuc_cha = $this->input->post('madanhmuc_cha');
            $this->baiviet->ID = $id;
            $this->baiviet->TieuDe = $tieude;
            $this->baiviet->NoiDungBaiViet_Viet = $noidungbaiviet;
            $this->baiviet->MaDanhMuc = $madanhmuc;
            $this->baiviet->MaDanhMuc_Cha = $madanhmuc_cha;
            $this->baiviet->NgayVietBai = date('Y-m-d h:m:s');
            $result = $this->baiviet->UpdateBaiViet(); 
            if($result)
                echo 'true';
            else           
                echo 'false';
        }
    }
    
    public function CapNhatHienThi()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : "";
        $madanhmuc = isset($_POST['maDanhMuc']) ? $_POST['maDanhMuc'] : "";
        $madanhmuc_cha = isset($_POST['maDanhMucCha']) ? $_POST['maDanhMucCha'] : "";
        $isDisplay = isset($_POST['isDisplay']) ? $_POST['isDisplay'] : "";
        if($id != "" && $madanhmuc != "" && $madanhmuc_cha != "" && $isDisplay != "")
        {
            $this->load->model('admin/managebaiviet','baiviet');
            $this->baiviet->ID = $id;
            $this->baiviet->MaDanhMuc = $madanhmuc;
            $this->baiviet->MaDanhMuc_Cha = $madanhmuc_cha;
            $this->baiviet->HienThi = $isDisplay;
            $result = $this->baiviet->UpdateHienThiBaiViet();
            if($result) echo 'true'; else echo 'false';
        }
        else
        {
            echo 'false';
        }
    }
}
?>